/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJPanel.java
 *
 * Created on 02-feb-2010, 19:09:40
 */

package tpdisenio2009.mvp.display;

import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableModel;
import tpdisenio2009.model.beans.FantasyTeamPlayer;
import tpdisenio2009.model.beans.FantasyTeamScoreByTournamentDate;
import tpdisenio2009.model.beans.SoccerPlayer;
import tpdisenio2009.model.beans.TournamentDate;
import tpdisenio2009.model.beans.User;

/**
 *
 * @author Pablito
 */
public class TournamentDateWinnersViewImpl extends javax.swing.JPanel implements TournamentDateWinnersView {

    /** Creates new form NewJPanel */
    public TournamentDateWinnersViewImpl() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelFecha = new javax.swing.JLabel();
        jComboBoxFecha = new javax.swing.JComboBox();
        jLabelNombre = new javax.swing.JLabel();
        jLabelMisPuntos = new javax.swing.JLabel();
        jLabelPuntos = new javax.swing.JLabel();
        jLabelMiPosicion = new javax.swing.JLabel();
        jLabelPosicion = new javax.swing.JLabel();
        jLabelPagina = new javax.swing.JLabel();
        jSpinnerPagina = new javax.swing.JSpinner();
        jScrollPaneTabla = new javax.swing.JScrollPane();
        jTableTabla = new javax.swing.JTable();
        jButtonVerListado = new javax.swing.JButton();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(tpdisenio2009.Tpdisenio2009App.class).getContext().getResourceMap(TournamentDateWinnersViewImpl.class);
        jLabelFecha.setText(resourceMap.getString("jLabelFecha.text")); // NOI18N
        jLabelFecha.setName("jLabelFecha"); // NOI18N

        jComboBoxFecha.setToolTipText(resourceMap.getString("jComboBoxFecha.toolTipText")); // NOI18N
        jComboBoxFecha.setName("jComboBoxFecha"); // NOI18N

        jLabelNombre.setFont(resourceMap.getFont("jLabelNombre.font")); // NOI18N
        jLabelNombre.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabelNombre.setText(resourceMap.getString("jLabelNombre.text")); // NOI18N
        jLabelNombre.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jLabelNombre.setName("jLabelNombre"); // NOI18N

        jLabelMisPuntos.setText(resourceMap.getString("jLabelMisPuntos.text")); // NOI18N
        jLabelMisPuntos.setName("jLabelMisPuntos"); // NOI18N

        jLabelPuntos.setFont(resourceMap.getFont("jLabelPuntos.font")); // NOI18N
        jLabelPuntos.setText(resourceMap.getString("jLabelPuntos.text")); // NOI18N
        jLabelPuntos.setName("jLabelPuntos"); // NOI18N

        jLabelMiPosicion.setText(resourceMap.getString("jLabelMiPosicion.text")); // NOI18N
        jLabelMiPosicion.setName("jLabelMiPosicion"); // NOI18N

        jLabelPosicion.setFont(resourceMap.getFont("jLabelPosicion.font")); // NOI18N
        jLabelPosicion.setText(resourceMap.getString("jLabelPosicion.text")); // NOI18N
        jLabelPosicion.setName("jLabelPosicion"); // NOI18N

        jLabelPagina.setText(resourceMap.getString("jLabelPagina.text")); // NOI18N
        jLabelPagina.setName("jLabelPagina"); // NOI18N

        jSpinnerPagina.setModel(new javax.swing.SpinnerNumberModel(1, 1, 10, 1));
        jSpinnerPagina.setToolTipText(resourceMap.getString("jSpinnerPagina.toolTipText")); // NOI18N
        jSpinnerPagina.setName("jSpinnerPagina"); // NOI18N

        jScrollPaneTabla.setName("jScrollPaneTabla"); // NOI18N

        jTableTabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Pos", "Apellido y Nombre", "Equipo", "Puntos", "Jugador con mejor puntaje"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableTabla.setName("jTableTabla"); // NOI18N
        jTableTabla.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jTableTabla.getTableHeader().setReorderingAllowed(false);
        jScrollPaneTabla.setViewportView(jTableTabla);
        jTableTabla.getColumnModel().getColumn(0).setMinWidth(30);
        jTableTabla.getColumnModel().getColumn(0).setPreferredWidth(35);
        jTableTabla.getColumnModel().getColumn(0).setMaxWidth(40);
        jTableTabla.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("jTableTabla.columnModel.title0")); // NOI18N
        jTableTabla.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("jTableTabla.columnModel.title1")); // NOI18N
        jTableTabla.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("jTableTabla.columnModel.title2")); // NOI18N
        jTableTabla.getColumnModel().getColumn(3).setMinWidth(50);
        jTableTabla.getColumnModel().getColumn(3).setPreferredWidth(60);
        jTableTabla.getColumnModel().getColumn(3).setMaxWidth(70);
        jTableTabla.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("jTableTabla.columnModel.title3")); // NOI18N
        jTableTabla.getColumnModel().getColumn(4).setHeaderValue(resourceMap.getString("jTableTabla.columnModel.title4")); // NOI18N

        jButtonVerListado.setText(resourceMap.getString("jButtonVerListado.text")); // NOI18N
        jButtonVerListado.setToolTipText(resourceMap.getString("jButtonVerListado.toolTipText")); // NOI18N
        jButtonVerListado.setName("jButtonVerListado"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 596, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPaneTabla, javax.swing.GroupLayout.Alignment.CENTER, javax.swing.GroupLayout.DEFAULT_SIZE, 576, Short.MAX_VALUE)
                        .addComponent(jButtonVerListado, javax.swing.GroupLayout.Alignment.CENTER)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(jLabelFecha)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jComboBoxFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jLabelNombre, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGap(18, 18, 18)
                            .addComponent(jLabelMisPuntos)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jLabelPuntos)
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(jLabelPagina)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jSpinnerPagina, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabelMiPosicion)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jLabelPosicion)))))
                    .addContainerGap()))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 293, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabelFecha)
                        .addComponent(jComboBoxFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabelNombre)
                        .addComponent(jLabelMisPuntos)
                        .addComponent(jLabelPuntos)
                        .addComponent(jLabelMiPosicion)
                        .addComponent(jLabelPosicion))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jSpinnerPagina, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabelPagina))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jScrollPaneTabla, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jButtonVerListado)
                    .addContainerGap()))
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonVerListado;
    private javax.swing.JComboBox jComboBoxFecha;
    private javax.swing.JLabel jLabelFecha;
    private javax.swing.JLabel jLabelMiPosicion;
    private javax.swing.JLabel jLabelMisPuntos;
    private javax.swing.JLabel jLabelNombre;
    private javax.swing.JLabel jLabelPagina;
    private javax.swing.JLabel jLabelPosicion;
    private javax.swing.JLabel jLabelPuntos;
    private javax.swing.JScrollPane jScrollPaneTabla;
    private javax.swing.JSpinner jSpinnerPagina;
    private javax.swing.JTable jTableTabla;
    // End of variables declaration//GEN-END:variables

    public void setDates(List<TournamentDate> dates) {
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        for (TournamentDate date : dates)
            model.addElement( date );
        jComboBoxFecha.setModel( model );
    }

    public TournamentDate getDate() {
        DefaultComboBoxModel model = (DefaultComboBoxModel) jComboBoxFecha.getModel();
        return (TournamentDate) model.getSelectedItem();
    }

    public void addDateChangeListener(ItemListener listener) {
        jComboBoxFecha.addItemListener( listener );
    }

    public void removeDateChangeListener(ItemListener listener) {
        jComboBoxFecha.removeItemListener( listener );
    }

    public void setFantasyTeamName(String fantasyTeamName) {
        jLabelNombre.setText( fantasyTeamName );
    }

    public void setOwnPoints(int points) {
        jLabelPuntos.setText( String.valueOf( points ) );
    }

    public void setOwnPosition(int position) {
        jLabelPosicion.setText( String.valueOf( position ) );
    }

    public void setPage(int page) {
        jSpinnerPagina.setValue( new Integer( page ) );
    }

    public int getPage() {
        Integer value = (Integer) jSpinnerPagina.getValue();
        return value == null? 0 : value.intValue();
    }

    public void setMaxPage(int page) {
        SpinnerNumberModel model = new SpinnerNumberModel( getPage(), 1, page, 1 );
        jSpinnerPagina.setModel( model );
    }

    public void addPageChangeListener(ChangeListener listener) {
        jSpinnerPagina.addChangeListener( listener );
    }

    public void removePageChangeListener(ChangeListener listener) {
        jSpinnerPagina.removeChangeListener( listener );
    }

    public void loadFantasyTeams( List<FantasyTeamScoreByTournamentDate> fantasyTeamScores, List<FantasyTeamPlayer> bestFantasyTeamPlayers) {
        DefaultTableModel model = (DefaultTableModel) jTableTabla.getModel();
        model.setRowCount( 0 );

        int pos = 1;
        for ( FantasyTeamScoreByTournamentDate fantasyTeam : fantasyTeamScores ) {
            User user = fantasyTeam.getFantasyTeam().getUser();
            String[] values = new String[5];
            values[0] = String.valueOf( pos++ );
            values[1] = user.getLastName() + ", " + user.getFirstName();
            values[2] = fantasyTeam.getFantasyTeam().getName();            
            values[3] = String.valueOf( fantasyTeam.getScore() );
            
            SoccerPlayer best = bestFantasyTeamPlayers.get(pos-2).getSoccerPlayer();
            values[4] = best.getLastName() + ", " + best.getFirstName() + " (" + best.getRealTeam().getName() + ")";

            model.addRow( values );
        }
    }

    public void addLookPrinterListListener(ActionListener listener) {
        jButtonVerListado.addActionListener( listener );
    }

    public void removeLookPrinterListListener(ActionListener listener) {
        jButtonVerListado.removeActionListener( listener );
    }

    public void showNotification( String notifiactionMessage, String notificationMessageTitle ) {
        JOptionPane.showMessageDialog( this,
                notifiactionMessage,
                notificationMessageTitle,
                JOptionPane.INFORMATION_MESSAGE);
    }

    public JComponent asWidget() {
        return this;
    }
}
